package org.steve.betterattack.PluginCommandExecutor;

import net.minecraft.server.v1_16_R3.NBTTagCompound;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.steve.betterattack.Abilities.ArrowRain;
import org.steve.betterattack.BetterAttack;
import org.steve.betterattack.CraftingRecipe;

import java.io.IOException;
import java.util.Map;

public class AdminCommandExecutor implements CommandExecutor {
    private final BetterAttack plugin;

    public AdminCommandExecutor(BetterAttack plugin){
        this.plugin=plugin;
    }

    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
        if(args.length==0){
            return false;
        }
        switch(args[0]){
            default:
                return false;
            case "help":
                commandSender.sendMessage(new String[]{
                        "betterattackadmin 帮助页面，可以使用别称/baa",
                        "baa help 显示帮助",
                        "baa give <player> <item>",
                        "baa exp <give|set> <value>",
                        "baa about 查询作者信息",
                        "baa list <item|recipe> [ignore-upgrade] 显示所有物品和武器",
                        "baa reload 重启插件",
                        "以下内容仅适用于开发版本：（普通版本也可以调用，但不建议/会有问题）",
                        "baa reset恢复默认的config"
                });
                break;
            case "give":
                if(args.length!=3){
                    return false;
                }
                Player player= Bukkit.getPlayer(args[1]);
                if (player != null){
                    if (plugin.RegisteredItems.containsKey(args[2])){
                        if (player.getInventory().addItem(plugin.RegisteredItems.get(args[2]).getItemStack()).keySet().stream().count()!=0){
                            commandSender.sendMessage("玩家背包没有空间");
                        }
                        else{
                            commandSender.sendMessage("给予"+args[1]+" 1*"+args[2]);
                        }
                    }
                    else if (plugin.RegisteredWeapons.containsKey(args[2])){
                        if (player.getInventory().addItem(plugin.RegisteredWeapons.get(args[2]).getItemStack(0)).keySet().stream().count()!=0){
                            commandSender.sendMessage("玩家背包没有空间");
                        }
                    }

                    else{
                        commandSender.sendMessage("物品不存在！");
                    }
                    break;
                }
                commandSender.sendMessage("找不到玩家！");
                break;
            case "reset":
                plugin.saveResource("config.yml",true);
                plugin.getLogger().info("已重置config.yml");
                commandSender.sendMessage("已重置config.yml，请用baa reload重启插件");
                break;
            case "reload":
                plugin.onDisable();
                plugin.reloadConfig();
                plugin.onEnable();
                commandSender.sendMessage("reload completed.");
                plugin.getLogger().info("reload completed.");
                break;
            case "about":
                commandSender.sendMessage(BetterAttack.pluginInfo);
                break;
            case "list":
                if(args.length<2) {
                    commandSender.sendMessage("参数不足");
                    break;
                }
                if(args[1]=="item"){
                    commandSender.sendMessage("武器数量："+plugin.RegisteredWeapons.keySet().stream().count());
                    for(String weapon:plugin.RegisteredWeapons.keySet()){
                        commandSender.sendMessage(weapon);
                    }
                    commandSender.sendMessage("物品数量："+plugin.RegisteredItems.keySet().stream().count());
                    for(String item:plugin.RegisteredItems.keySet()){
                        commandSender.sendMessage(item);
                    }
                }
                if(args[1]=="recipe") {
                    for (String id : plugin.RegisteredCraftingRecipe.keySet()) {
                        if(args.length>2&&args[2]=="ignore-upgrade"){
                            if(plugin.RegisteredCraftingRecipe.get(id).isUpgrade)continue;
                        }
                        commandSender.sendMessage(id);
                    }
                }
                break;
            case "info":
                if (args.length<3) {
                    commandSender.sendMessage("参数不足");
                    break;
                }
                switch (args[1]){
                    case "recipe":
                        commandSender.sendMessage(plugin.RegisteredCraftingRecipe.get(args[2]).getInfo());
                        break;
                    default:
                        commandSender.sendMessage("无效参数");
                        break;
                }
                break;
            case "test":
                if (!(commandSender instanceof Player))break;
                Player player1=(Player)commandSender;
                switch (args[1]) {
                    case "ar":
                        ArrowRain ar=new ArrowRain(plugin,10,0,"ArrowRain");
                        ar.execute(plugin.getServer().getPlayer(args[2]),player1);
                        break;
                    case "inventory":
                        Inventory inventory= Bukkit.createInventory(null,45);
                        player1.closeInventory();
                        player1.openInventory(inventory);
                    default:
                        commandSender.sendMessage("test: 无法解析");
                        break;
                }
                break;

        }
        return true;
    }
}
